<?php

header("Content-type: text/html; charset=utf-8");

$page = $_GET['page']; // get the requested page 
$limit = $_GET['rows']; // get how many rows we want to have into the grid 
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
$sord = $_GET['sord']; // get the direction 

$admin = false;
require_once('auth_req.inc.php');

$rs = $dbh->query('select count(*) from network_map');

if ($rec = $rs->fetchAll(PDO::FETCH_COLUMN, 0))
{
    $count = $rec[0];
}
else
{
    $count = 0;
}

if( $count > 0 ) 
{ 
    $total_pages = ceil($count/$limit); 
} 
else 
{ 
    $total_pages = 0; 
} 

if ($page > $total_pages) 
    $page=$total_pages;


$start = $limit * $page - $limit;

if ($start < 0) 
    $start = 0;

$order_by_cause = 'order by ';

$idx_array = array('c_id' => 'nm.c_id', 'g_name' => 'gr.name', 'location' => 'location');

$idx = $idx_array[$sidx];

if (isset($idx) && (($sord == 'asc') || ($sord == 'desc')))
{                     
    $order_by_cause .= $idx . ' ' . $sord;
}
else
{
    $order_by_cause = '';
}


$sth = $dbh->prepare('select nm.c_id, gr.name as g_name, nm.location, nm.online, nm.job_mode, nm.prog, nm.phase, ' .  
                     'nm.fault, nm.fault_red, nm.fault_yellow, nm.fault_green, nm.cont_scan_time, nm.cont_local_time '.   
                     'from network_map nm, groups gr ' .  
                     'where gr.g_id = nm.g_id ' . $order_by_cause . ' limit :start,:limit');

$sth->bindValue(':start', intval($start), PDO::PARAM_INT);
$sth->bindValue(':limit', intval($limit), PDO::PARAM_INT);

$sth->execute();

$response = new stdClass();

$response->page = $page; 
$response->total = $total_pages; 
$response->records = $count; 

$alldb = $sth->fetchAll(PDO::FETCH_ASSOC);

foreach( $alldb as $key => $val)
{
    $super_state = 0;

    if ($val['online'] == 0)
    {
        $state = '<span style="color:gray">Нет связи.</span>';
        $prog = '?';
        $phase = '?';
        $local_time = '?';
        $state = 'Нет связи';
        $fault = '?';
    }
    else
    {
        switch($val['job_mode'])
        {
                case 0:
                    $super_state = 1;
                    $state = '<span style="color:green">Локальное управление</span>';
                break;

                case 2:
                case 1:
                    $super_state = 3;
                    $state = '<span style="color:green">Диспетчерское управление</span>';
                break;

                case 6:
                    $super_state = 2;
                    $state = '<span style="color:green">Вызов программы</span>';
                break;

                case 3:
                    $state = '<span style="color:green">Управление с ВПУ</span>';
                break;

                case 5:
                    $state = '<span style="color:green">Работы на СО</span>';
                break;

                case 11:
                    $state = '<span style="color:red">Нет питания !</span>';
                break;

                default:
                    $state = '<span style="color:red">Неизвестный режим</span>';
                break;
            }

        if ($val['fault'] == 0)
        {
            $fault = '&mdash;';
        }
        else
        {
            $fault = '<span style="color:red">';

            switch ($val['fault'])
            {
                case 1:
                    $fault .= 'КЗ ';
                break;

                case 2:
                    $fault .= 'Перегрузка ';
                break;

                case 3:
                    $fault .= 'Недогрузка ';
                break;

                case 4:
                    $fault .= 'Перегорел ';
                break;

                case 5:
                    $fault .= 'Конфликт ';
                break;

            }

            if ($val['fault_red'] > 0)
            {
                $fault .= 'красн. Напр: '.$val['fault_red'];
            }
            else if ($val['fault_yellow'] > 0)
            {
                $fault .= 'желт. Напр: '.$val['fault_yellow'];
            }
            else if ($val['fault_green'] > 0)
            {
                $fault .= 'зел. Напр: '.$val['fault_green'];
            }

            $fault .= '</span>';
        }

        $prog = $val['prog'];
        $phase = $val['phase'];
        $local_time = $val['cont_local_time'];
    }
 
    $response->rows[$key]['id'] = $val['c_id'];
    $response->rows[$key]['cell'] = array( $val['c_id'], $val['g_name'],$val['location'], $state, $fault, $prog, $phase, $val['cont_scan_time'], $local_time, $super_state);
}

echo json_encode($response);

?>
                                                              